#!/bin/sh
#check whether is a rt working directory
rt_noconf()
{
    echo "This is not rt working directory. Please add rt config file \".rt.config\" . Format:"
    echo "dst_ip="
    echo "dst_port="
    echo "dst_dir="
    echo "dst_user="
    echo "dst_pwd="
    echo "jump_ip="
    echo "jump_port="
    echo "jump_tmpdir="
    echo "jump_user="
    echo "jump_pwd="
}

rt_exec()
{
    for i in `seq 1 $#`
    do
        line="$(eval echo "\${$i}")";
        #echo "$line"
        #echo  ${line%%=*}=${line#*=}
        eval  ${line%%=*}=${line#*=}
    done
}

rt_exec_init()
{
    for i in `seq 1 $#`
    do
        line="$(eval echo "\${$i}")";
        #echo "$line"
        #echo  ${line%%=*}=${line#*=}
        eval  rt_${line%%=*}=${line#*=}
    done
}


rt_init()
{
    export LC_ALL="zh_CN.gbk"
    rt_exec_init `rtinfo -j|grep =`
    #mkdir -p /tmp/rt
    #rt_init_tmpdir="/tmp/rt/rt_init_`date +%s%N`"
    #rtinfo -j > $rt_init_tmpdir
    #while read line
    #do
    #    #echo "$line"
    #    #echo  ${line%%=*}=${line#*=}
    #    eval  rt_${line%%=*}=${line#*=}
    #done<  $rt_init_tmpdir
    #rm -f $rt_init_tmpdir
}

rt_diff_file()
{
    #echo diff_file "$1"
    file_para="$1"
    ll_local_tmpdir="/tmp/rt/rtdiff_local_`date +%s%N`"
    ll_remote_tmpdir="/tmp/rt/rtdiff__remote_`date +%s%N`"
    mkdir -p $ll_local_tmpdir
    mkdir -p $ll_remote_tmpdir

    #local file
    #echo cp $file_para $ll_local_tmpdir
    cp $file_para $ll_local_tmpdir
    #echo cat $ll_local_tmpdir/$file_para
    #cat $ll_local_tmpdir/$file_para

    #remote file
    #echo rtinfo > $ll_remote_tmpdir/.rt.config
    rtinfo > $ll_remote_tmpdir/.rt.config
    #echo rt -r $ll_remote_tmpdir/$file_para
    #rt -r $ll_remote_tmpdir/$file_para 
    rt -r $ll_remote_tmpdir/$file_para > /dev/null
    #echo cat $ll_remote_tmpdir/$file_para
    #cat $ll_remote_tmpdir/$file_para

    #echo "diff $ll_local_tmpdir/$file_para $ll_remote_tmpdir/$file_para"
    diff $ll_local_tmpdir/$file_para $ll_remote_tmpdir/$file_para
    #echo rm -rf $ll_local_tmpdir $ll_remote_tmpdir
    rm -rf $ll_local_tmpdir $ll_remote_tmpdir
}

rt_diff_ls()
{
    #echo diff_ls "$*"
    ls_para="$*"
    ll_local_tmpfile="/tmp/rt/rtdiff_local_`date +%s%N`"
    ll_remote_tmpfile="/tmp/rt/rtdiff__remote_`date +%s%N`"

    #local ls
    #echo "ls -l  $ls_para 2>/dev/null|awk '{if(\$8~/.*:.*/){print \$9,\$6,\$7,\$8,\$5}}' > $ll_local_tmpfile"
    #ls -l  $ls_para  2>/dev/null|awk '{if($8~/.*:.*/){print $9,$6,$7,$8,$5}}' 
    export LC_ALL=en_US ;export TIME_STYLE='+%b %d %H:%M' ;ls -l  $ls_para  2>/dev/null|awk '{if($8~/.*:.*/){print $9,$6,$7,$8,$5}}' > $ll_local_tmpfile
    #cat $ll_local_tmpfile

    #remote ls
    #echo "rtrun ls  -l $ls_para  |awk '{if(\$8~/.*:.*/){print \$9,\$6,\$7,\$8,\$5}}' > $ll_remote_tmpfile"
    #rtrun ls -l  $ls_para |awk '{if($8~/.*:.*/){print $9,$6,$7,$8,$5}}' 
    rtrun "export LC_ALL=en_US ;export TIME_STYLE='+%b %d %H:%M' ;ls -l  $ls_para" |grep -v TIME_STYLE|awk '{if($8~/.*:.*/){print $9,$6,$7,$8,$5}}' > $ll_remote_tmpfile
    sed 's/\[[0-9][0-9]m//g' -i $ll_remote_tmpfile
    sed 's/\[[0-9]m//g' -i $ll_remote_tmpfile
    sed 's/\[[0-9][0-9];[0-9][0-9]m//g' -i $ll_remote_tmpfile
    sed 's///g' -i $ll_remote_tmpfile
    #cat $ll_remote_tmpfile
    #vim $ll_remote_tmpfile

    sort $ll_local_tmpfile  |awk '{printf "%10s %3s %2s %5s %s\n",$5,$2,$3,$4,$1}'> $ll_local_tmpfile.sort
    sort $ll_remote_tmpfile |awk '{printf "%10s %3s %2s %5s %s\n",$5,$2,$3,$4,$1}'> $ll_remote_tmpfile.sort
    #echo "diff $ll_local_tmpfile.sort $ll_remote_tmpfile.sort"
    diff $ll_local_tmpfile.sort $ll_remote_tmpfile.sort
    rm $ll_local_tmpfile $ll_remote_tmpfile
    rm $ll_local_tmpfile.sort $ll_remote_tmpfile.sort
}
